<?php

use think\migration\Migrator;
use think\migration\db\Column;

class CreateSystemConfigsTable extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('system_configs', [
            'id' => false,
            'primary_key' => ['id'],
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_unicode_ci',
            'comment' => '系统配置表'
        ]);

        $table->addColumn('id', 'integer', [
            'identity' => true,
            'signed' => false,
            'comment' => '主键ID'
        ])
        ->addColumn('config_key', 'string', [
            'limit' => 100,
            'null' => false,
            'comment' => '配置键名'
        ])
        ->addColumn('config_value', 'text', [
            'null' => true,
            'comment' => '配置值'
        ])
        ->addColumn('config_name', 'string', [
            'limit' => 200,
            'null' => false,
            'comment' => '配置名称'
        ])
        ->addColumn('config_type', 'string', [
            'limit' => 50,
            'null' => false,
            'default' => 'text',
            'comment' => '配置类型(text,textarea,select,file等)'
        ])
        ->addColumn('config_group', 'string', [
            'limit' => 50,
            'null' => false,
            'default' => 'system',
            'comment' => '配置分组'
        ])
        ->addColumn('description', 'string', [
            'limit' => 500,
            'null' => true,
            'comment' => '配置描述'
        ])
        ->addColumn('sort_order', 'integer', [
            'signed' => false,
            'null' => false,
            'default' => 0,
            'comment' => '排序'
        ])
        ->addColumn('is_required', 'boolean', [
            'null' => false,
            'default' => false,
            'comment' => '是否必填'
        ])
        ->addColumn('is_encrypted', 'boolean', [
            'null' => false,
            'default' => false,
            'comment' => '是否加密存储'
        ])
        ->addColumn('created_at', 'datetime', [
            'null' => false,
            'default' => 'CURRENT_TIMESTAMP',
            'comment' => '创建时间'
        ])
        ->addColumn('updated_at', 'datetime', [
            'null' => false,
            'default' => 'CURRENT_TIMESTAMP',
            'comment' => '更新时间'
        ])
        ->addIndex(['config_key'], ['unique' => true])
        ->addIndex(['config_group'])
        ->create();

        // 插入默认配置数据
        $this->insert('system_configs', [
            [
                'config_key' => 'xiaofeng_order_token',
                'config_value' => '',
                'config_name' => '晓风订单Token',
                'config_type' => 'text',
                'config_group' => 'external_api',
                'description' => '晓风订单系统的访问Token',
                'sort_order' => 1,
                'is_required' => true,
                'is_encrypted' => true,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ],
            [
                'config_key' => 'xiaofeng_create_order_token',
                'config_value' => '',
                'config_name' => '晓风下单Token',
                'config_type' => 'text',
                'config_group' => 'external_api',
                'description' => '晓风下单系统的访问Token',
                'sort_order' => 2,
                'is_required' => true,
                'is_encrypted' => true,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s')
            ]
        ]);
    }
} 